package com.goddess.yeecolor.view;

import android.support.v4.view.ViewPager;
import android.view.View;

/**
 * Created by recker on 16/5/11.
 */
public class ZoomOutPageTransformer implements ViewPager.PageTransformer  {


    private static final float MIN_SCALE = 0.9f;
    private static final float MIN_ALPHA = 1.0f;

    private static float defaultScale = 0.9f;

    public void transformPage(View view, float position) {
        int pageWidth = view.getWidth();
        int pageHeight = view.getHeight();

        if (position < -1) {
            // 这个页面是关闭屏幕左边。
            view.setAlpha(0);
            view.setScaleX(defaultScale);
            view.setScaleY(defaultScale);
        } else if (position <= 1) {
            // 修改默认的幻灯片转换以缩小页面
            float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
            float vertMargin = pageHeight * (1 - scaleFactor) / 2;
            float horzMargin = pageWidth * (1 - scaleFactor) / 2;
            if (position < 0) {
                view.setTranslationX(horzMargin - vertMargin / 0.6f);
            } else {
                view.setTranslationX(-horzMargin + vertMargin / 0.6f);
            }

            // 量表的下一页（min_scale和1之间）
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);

            // 淡出页面相对于它的大小
            view.setAlpha(MIN_ALPHA +
                    (scaleFactor - MIN_SCALE) /
                            (1 - MIN_SCALE) * (1 - MIN_ALPHA));

        } else {
            // 此页是关闭屏幕右侧。
            view.setAlpha(0);
            view.setScaleX(defaultScale);
            view.setScaleY(defaultScale);
        }
    }

}
